package cn.bj.yu.bq;


import java.util.Collection;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author 周宇
 * @create 2020-08-13 16:52
 */
public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueueDemo example = new BlockingQueueDemo();
        // 抛出异常
        example.Throw_an_exception();
        // 返回特殊值
        //example.Returns_a_special_value();
        // 代码测试（一直阻塞）
        //example.continued_block();
        // 超时退出
        //example.Timeout_exit();
    }

    /**
     * 抛出异常
     */
    public void Throw_an_exception() {
        //队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        System.out.println(blockingQueue.element());
        System.out.println(blockingQueue.element());

        System.out.println("=====================");
        //java.lang.IllegalStateException: Queue full: 抛出异常 队列已满
        //System.out.println(blockingQueue.add("d"));
        System.out.println(blockingQueue.remove());//a 先进先出 弹
        System.out.println(blockingQueue.remove());//b
        System.out.println(blockingQueue.remove());//c
        //java.util.NoSuchElementException:抛出异常，没有这样的元素例外
        //System.out.println(blockingQueue.remove());
    }


    /**
     * 返回特殊值
     */
    public void Returns_a_special_value() {
        //队列的大小 3
        ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d"));//返回false 不抛出异常
        System.out.println("=====================");
        System.out.println(blockingQueue.peek());// 检测队列队首元素！

        // public E poll()
        System.out.println(blockingQueue.poll());//a
        System.out.println(blockingQueue.poll());//b
        System.out.println(blockingQueue.poll());//c
        System.out.println(blockingQueue.poll());//null
    }

    /**
     * 一直阻塞
     */
    public void continued_block() throws InterruptedException {
        // 队列大小
        ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3);

        // 一直阻塞
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        //blockingQueue.put("d");//该线程 一直阻塞，不打印

        System.out.println(blockingQueue.take());//a
        System.out.println(blockingQueue.take());//b
        System.out.println(blockingQueue.take());//c
        //System.out.println(blockingQueue.take());//阻塞不停止等待
    }

    public void Timeout_exit() throws InterruptedException {
        // 队列大小
        ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3);
        // 一直阻塞
        blockingQueue.offer("a");
        blockingQueue.offer("b");
        blockingQueue.offer("c");
        blockingQueue.offer("d", 3L, TimeUnit.SECONDS);//等待3秒超时

        System.out.println(blockingQueue.poll());//a
        System.out.println(blockingQueue.poll());//b
        System.out.println(blockingQueue.poll());//c
        System.out.println(blockingQueue.poll(3L, TimeUnit.SECONDS));//阻塞不停止等待
    }
}